package com.example.androidstart.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.androidstart.BiggerImageActivity;

import java.io.IOException;

public class CustomFrameLayout extends FrameLayout {

    private BiggerView mBiggerView;

    public CustomFrameLayout(@NonNull Context context) {
        super(context);
    }

    public CustomFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private Canvas mCanvas = new Canvas();

    private Bitmap mRealTimeBitmap;

    private Bitmap mSrcBitmap;

    private static final String TAG = "CustomFrameLayout";

    private Paint mPaint;

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(5);

        int width = getWidth();
        int height = getHeight();
        Log.i(TAG, "init: width height" + width + " height " + height);
        mRealTimeBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas.setBitmap(mRealTimeBitmap);
        mBiggerView.setBitmap(mRealTimeBitmap);
        try {
            mSrcBitmap = BitmapFactory.decodeStream(CustomFrameLayout.this.getResources().getAssets().open("img_1.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        init();
    }

    private ImageView indexPoint;

    @Override
    protected void onDraw(Canvas canvas) {

        mCanvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);

        mCanvas.drawCircle(downX, downY, 30, mPaint);
        Log.i(TAG, "onDraw: framelayout");
        canvas.drawBitmap(mRealTimeBitmap, 0, 0, mPaint);
        super.onDraw(canvas);
    }

    private float downX;

    private float downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            downX = event.getX();
            downY = event.getY();
            mBiggerView.downXY(downX, downY);
            indexPoint.setX(downX);
            indexPoint.setY(downY);
            mBiggerView.invalidate();
            requestLayout();
//            mBiggerView.requestLayout();
        }
        return true;
    }

    public void setBiggerView(BiggerView mBiggerView) {
        this.mBiggerView = mBiggerView;
    }

    public void setIndexPoint(ImageView indexPoint) {
        this.indexPoint = indexPoint;
    }
}
